/**
  @(#)$Id: AL_File.cpp 69 2013-10-08 03:03:33Z xiaoting $
  @brief    	AL_File File operations

  @author		$Author: xiaoting $
  @date			$Date: 2013-10-08 11:03:33 +0800 (周二, 08 十月 2013) $
  @version		$Revision: 69 $
  @url			$URL: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_StandardLib/AL_File.cpp $
  @header		$Header: https://svn.code.sf.net/p/xiaoting/game/trunk/MyProject/AL_StandardLib/AL_File.cpp 69 2013-10-08 03:03:33Z xiaoting $
 */
#include "stdafx.h"

#ifndef CXX_AL_FILE_H
#include "AL_File.h"
#endif

#ifndef CXX_AL_ERROR_H
#include "AL_Error.h"
#endif

// AL_ErrorLog Recording
static const NCHAR g_szErrorFileInfo[] = NTEXT("$Id: AL_File.cpp 69 2013-10-08 03:03:33Z xiaoting $");
///////////////////////////////////////////////////////////////////////////
//			AL_File
///////////////////////////////////////////////////////////////////////////
/**
* Construction
*
* @param
* @return
*/
AL_File::AL_File():
m_pfstream(NULL)
{
	m_pfstream = new NCHARfstream;
}

/**
* Construction
*
* @param		<IN> const NCHAR* cpFilePath
* @param		<IN> WORD wOpenModel
static const WORD MODEL_IN			= std::ios::in;			// 0x01, for reading the file does not exist, create (ifstream Open default)
static const WORD MODEL_OUT			= std::ios::out;		// 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
static const WORD MODEL_ATE			= std::ios::ate;		// 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
static const WORD MODEL_APP			= std::ios::app;		// 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
static const WORD MODEL_TRUNC		= std::ios::trunc;		// 0x10, length of the file read and write before first truncated to 0 (default)
static const WORD MODEL_NOCREATE	= std::ios::_Nocreate;	// 0x20, file does not exist error, often used in combination and in or app
static const WORD MODEL_NOREPLACE	= std::ios::_Noreplace;	// 0x40, file exists generates an error when used in combination, often and out
static const WORD MODEL_BINARY		= std::ios::binary;		// 0x80, binary format file
* @param		<IN> WORD wAccess
static const WORD ACCESS_ORDINARY	= 0;					// 0: ordinary files, open access
static const WORD ACCESS_READONLY	= 1;					// 1: read-only file
static const WORD ACCESS_HIDDEN		= 2;					// 2: hidden file
static const WORD ACCESS_SYSTEM		= 4;					// 4: System Files
* @return
* @note
* @attention	The default way to open a file to open the binary file reading and writing
				The default file ordinary files, open access
*/
AL_File::AL_File(const NCHAR* cpFilePath, WORD wOpenModel, WORD wOpenAccess):
m_pfstream(NULL)
{
	m_pfstream = new NCHARfstream;
	Open(cpFilePath, wOpenModel, wOpenAccess);
}

/**
* Destruction
*
* @param
* @return
*/
AL_File::~AL_File(VOID)
{
	if (NULL == m_pfstream) {
		delete m_pfstream;
		m_pfstream = NULL;
	}
}

///////////////////////////////////Open the file///////////////////////////////////////
/**
* Open (Open the file)
*
* @param		<IN> const NCHAR* cpFilePath
* @param		<IN> WORD wOpenModel
					static const WORD MODEL_IN			= std::ios::in;			// 0x01, for reading the file does not exist, create (ifstream Open default)
					static const WORD MODEL_OUT			= std::ios::out;		// 0x02, for writing, the file does not exist, to create, if the file already exists, clear the original content (ofstream default Open)
					static const WORD MODEL_ATE			= std::ios::ate;		// 0x04, when the file is opened, the pointer in the file last. Can change the position of the pointer, often used in combination and in, out
					static const WORD MODEL_APP			= std::ios::app;		// 0x08, for writing, the file does not exist, to create, to write new content after the original contents of the file if the file already exists, the total in the final pointer position
					static const WORD MODEL_TRUNC		= std::ios::trunc;		// 0x10, length of the file read and write before first truncated to 0 (default)
					static const WORD MODEL_NOCREATE	= std::ios::_Nocreate;	// 0x20, file does not exist error, often used in combination and in or app
					static const WORD MODEL_NOREPLACE	= std::ios::_Noreplace;	// 0x40, file exists generates an error when used in combination, often and out
					static const WORD MODEL_BINARY		= std::ios::binary;		// 0x80, binary format file
* @param		<IN> WORD wAccess
					static const WORD ACCESS_ORDINARY	= 0;					// 0: ordinary files, open access
					static const WORD ACCESS_READONLY	= 1;					// 1: read-only file
					static const WORD ACCESS_HIDDEN		= 2;					// 2: hidden file
					static const WORD ACCESS_SYSTEM		= 4;					// 4: System Files
* @return VOID
* @note			Note the slash in the path name to dual-write, such as: "\\MyFiles\\ReadMe.txt"
* @attention	The default way to open a file to open the binary file reading and writing		
*/
VOID 
AL_File::Open(const NCHAR* cpFilePath, WORD wOpenModel, WORD wOpenAccess)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->open(cpFilePath, wOpenModel, wOpenAccess);
}

////////////////////////////////Check if successfully opened//////////////////////////////////////////
/**
* Good (Check if successfully opened)
*
* @param VOID
* @return BOOL
* @note
* @attention
*/
BOOL 
AL_File::Good(VOID)
{
	if (NULL == m_pfstream) {
		return FALSE;
	}
	return m_pfstream->good();

}

/**
* Fail (Check if successfully opened)
*
* @param VOID
* @return BOOL
* @note
* @attention
*/
BOOL 
AL_File::Fail(VOID)
{
	if (NULL == m_pfstream) {
		return FALSE;
	}
	return m_pfstream->fail();
}

/**
* IsOpen (Check if successfully opened)
*
* @param VOID
* @return BOOL
* @note			Generally use this method to determine if a file is open successfully
* @attention
*/
BOOL 
AL_File::IsOpen(VOID)
{
	if (NULL == m_pfstream) {
		return FALSE;
	}
	return m_pfstream->is_open();

}

/////////////////////////////////Reading and writing binary files/////////////////////////////////////////
/**
* Put (Reading and writing binary files)
*
* @param		<IN> NCHAR ch
* @return VOID
* @note			put () function to write to the stream a character prototype ofstream & put (char ch), 
				the use of relatively simple, such as file1.put ('c'); is to write to the stream a character 'c'.
* @attention
*/
VOID 
AL_File::Put(NCHAR ch)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->put(ch);
}

/**
* Get (Reading and writing binary files)
*
* @param		<OUT> NCHAR& ch
* @return VOID
* @note			put () corresponds to the form: ifstream & get (char & ch); function is to read a single character 
				from the stream, and the result is stored in the reference ch, if the end of the file, and returns 
				a null character. As file2.get (x); represents a character read from the file, and read characters 
				stored in x.
* @attention
*/
VOID 
AL_File::Get(NCHAR& ch)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->get(ch);
}

/**
* Get (Reading and writing binary files)
*
* @param		<OUT> NCHAR* pStr
* @param		<IN> DWORD dwGetNum
* @param		<IN> NCHAR chEndChar
* @return VOID
* @note			ifstream & get (char * buf, int num, char delim = '\ n'); this form to read characters into the 
				array pointed to by buf until reads num characters or encounter characters specified by delim, 
				ifdelim this parameter will use the default value of the newline character '\ n'. For example:
				file2.get (str1, 127, 'A') ;/ / read characters from a file into a string str1 terminate when 
				to encounter characters 'A' or read 127 characters.
* @attention
*/
VOID 
AL_File::Get(NCHAR* pStr, DWORD dwGetNum, NCHAR chEndChar)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->get(pStr, dwGetNum, chEndChar);
}


/**
* Read (Reading and writing binary files)
*
* @param		<OUT> NCHAR* buf
* @param		<IN> DWORD dwNum
* @return BOOL
* @note			Prototype: read (unsigned char * buf, int num); read () num characters to read from the file 
				cache buf points to the end of the file has not been read into the num characters can use member 
				functionsint gcount (); to get the actual number of characters read
* @attention
*/
VOID 
AL_File::Read(NCHAR* buf, DWORD dwNum)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->read(buf, dwNum);
}

/**
* Write (Reading and writing binary files)
*
* @param		<IN> NCHAR* buf
* @param		<IN> DWORD dwNum
* @return BOOL
* @note			Prototype: write (const unsigned char * buf, int num); write () from buf points to the cache 
				write num characters to the file, it is noteworthy cache type is unsigned char *, may sometimes
				be necessary type conversion.
* @attention
*/
VOID 
AL_File::Write(const NCHAR* buf, DWORD dwNum)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->write(buf, dwNum);
}

/**
* Eof End of file is read (Reading and writing binary files)
*
* @param VOID
* @return BOOL
* @note
* @attention
*/
BOOL 
AL_File::Eof(VOID)
{
	if (NULL == m_pfstream) {
		return FALSE;
	}
	return m_pfstream->eof();
}

/**
* Gcount The actual number of bytes read (Reading and writing binary files)
*
* @param VOID
* @return DWORD
* @note
* @attention
*/
DWORD 
AL_File::Gcount(VOID)
{
	if (NULL == m_pfstream) {
		return FALSE;
	}
	return m_pfstream->gcount();
}

/**
* Seekg Absolutely move (Reading and writing binary files)
*
* @param		<IN> DWORD dwSeek absolute move position
* @return VOID
* @note
* @attention	Input stream operation
*/
VOID 
AL_File::Seekg(DWORD dwSeek)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->seekg(dwSeek);
}

/**
* Seekg Relatively move (Reading and writing binary files)
*
* @param		<IN> DWORD dwSeek relatively move position
* @param		<IN> FILE_REFERENCE_POS eFileRefPos file reference position
					FILE_REFERENCE_POS_BEG = std::ios :: beg,	// 0: relative to the file header
					FILE_REFERENCE_POS_CUR = std::ios :: cur,	// 1: relative to the current position
					FILE_REFERENCE_POS_END = std::ios :: end,	// 2: relative to the end of the file
* @return VOID
* @note
* @attention	Input stream operation
*/
VOID 
AL_File::Seekg(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->seekg(dwSeek, eFileRefPos);
}

/**
* Tellg Returns the current pointer position (Reading and writing binary files)
*
* @param VOID
* @return VOID
* @note
* @attention	Input stream operation
*/
VOID 
AL_File::Tellg(VOID)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->tellg();
}

/**
* Seekp Absolutely move (Reading and writing binary files)
*
* @param		<IN> DWORD dwSeek absolute move position
* @return VOID
* @note
* @attention	Output stream operation
*/
VOID 
AL_File::Seekp(DWORD dwSeek)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->seekp(dwSeek);
}

/**
* Seekp Relatively move (Reading and writing binary files)
*
* @param		<IN> DWORD dwSeek relatively move position
* @param		<IN> FILE_REFERENCE_POS eFileRefPos file reference position
					FILE_REFERENCE_POS_BEG = std::ios :: beg,	// 0: relative to the file header
					FILE_REFERENCE_POS_CUR = std::ios :: cur,	// 1: relative to the current position
					FILE_REFERENCE_POS_END = std::ios :: end,	// 2: relative to the end of the file
* @return VOID
* @note
* @attention	Output stream operation
*/
VOID 
AL_File::Seekp(DWORD dwSeek, FILE_REFERENCE_POS eFileRefPos)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->seekp(dwSeek, eFileRefPos);
}

/**
* Tellp Returns the current pointer position (Reading and writing binary files)
*
* @param VOID
* @return VOID
* @note
* @attention	Output stream operation
*/
VOID 
AL_File::Tellp(VOID)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->tellp();
}


/////////////////////////////////////Close the file/////////////////////////////////////
/**
* Close (Close the file)
*
* @param VOID
* @return VOID
* @note
* @attention
*/
VOID 
AL_File::Close(VOID)
{
	if (NULL == m_pfstream) {
		return;
	}
	m_pfstream->close();
}

/* EOF */
